/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package engine.linkList;

/**
 *
 * @author hjp
 */
import java.io.Serializable;
import java.text.Collator;
import java.util.Locale;

public class MusicNode implements AbstractNode, Serializable {
    //todo 1.由于时间是以秒来计算的，故在显示的时候应当转换为 分：秒 的形式。转换公式如下：
    // time / 60 : time % 60
    //todo 2.由于文件大小是以字节为单位，故在显示的时候需要转换。
    String songName;
    String artist;
    String album;
    String filePath;
    int time;
    long size;
    boolean isDownload;

    MusicNode(String songName, String artist, String album, int time, long size){
        this.songName = songName;
        this.artist = artist;
        this.album = album;
        this.time = time;
        this.size = size;
        filePath = null;
        isDownload = false;
    }

    void setFilePath(String filePath){
        this.filePath = filePath;
    }

    @Override
    public String toString(){
        return songName +  "\n" + artist + "\n" + album + "\n" + time
                + "\n" + size;
    }
    
    public String getSongName(){
        return songName;
    }
    
    public String getArtist(){
        return artist;
    }
    
    public String getAlbum(){
        return album;
    }
    
    public String getFilePath(){
        return filePath;
    }
    
    public String getTime_String(){
        String minute = null;
        String second = null;
        //格式化输出时间
        if ((time / 60) < 10) {
            minute = "0" + String.valueOf(time / 60);
        }
        else{
            minute = String.valueOf(time / 60);
        }
        if ((time % 60) < 10) {
            second = "0" + String.valueOf(time % 60);
        }
        else{
            second = String.valueOf(time % 60);
        }
        return minute +  ":" +second;
    }
    
    public int getTime_Int(){
        return time;
    }
    
    public long getSize(){
        return size;
    }
    
    public boolean getIsDownload(){
        return isDownload;
    }
    
    /**
     * 判断给定的关键字是否匹配当前歌曲
     * @param key 待查找的关键字
     * @return 匹配，则返回true
     */
    boolean isMatch(String key){
        boolean flag = false;
        if (containsIgnoreCase(songName,key)){
            flag = true;
        }
        if (containsIgnoreCase(artist,key)){
            flag = true;
        }
        if (containsIgnoreCase(album,key)){
            flag = true;
        }
        return flag;
    }
    
    boolean containsIgnoreCase(String key1,String key2){
        char []keyArray1 = key1.toCharArray();
        char []keyArray2 = key2.toCharArray();
        int i = 0,j = 0;
        int k = i;
        while(i < key1.length() && j < key2.length()){
            if (keyArray1[i] == keyArray2[j]) {
                ++i;
                ++j;
            }
            else if(('a' <= keyArray2[j] && 'z' >= keyArray2[j]) || ('A' <= keyArray2[j] && 'Z' >= keyArray2[j])){
                char ch = keyArray2[j];
                char ch1;
                if (('a' <= keyArray2[j] && 'z' >= keyArray2[j])) {
                    ch1 = (char)(ch - 32);
                }
                else{
                    ch1 = (char)(ch + 32);
                }
                if (ch == keyArray1[i] || ch1 == keyArray1[i]) {
                    ++i;
                    ++j;
                }
                else{
                    i = ++k;
                    j = 1;
                }
            }
            else{
                i = ++k;
                j = 1;
            }
        }
        if (j == key2.length() ) {
            return true;
        }
        else{
            return false;
        }
    }
    
    int compare(MusicNode node, SortBy groupBy){
        String key1 = null;
        String key2 = null;
        Collator instance = Collator.getInstance(Locale.CHINA);
        switch (groupBy){
            case SONGNAME:{
                key1 = songName;
                key2 = node.songName;
                break;
            }
            case TIME:{
                key1 = String.valueOf(time);
                key2 = String.valueOf(node.time);
                break;
            }
            case SIZE:{
                key1 = String.valueOf(size);
                key2 = String.valueOf(node.size);
                break;
            }
            case ALBUM:{
                key1 = album;
                key2 = node.album;
                break;
            }
            case ARTIST:{
                key1 = artist;
                key2 = node.artist;
                break;
            }
        }
        return instance.compare(key1,key2);
    }

    boolean equal(MusicNode node){
        return songName.equals(node.songName) && artist.equals(node.artist)
                && album.equals(node.album) && filePath.equals(node.filePath)
                && time == node.time && size == node.size;
    }

//    void delete(){
//        songName = null;
//        artist = null;
//        album = null;
//        filePath = null;
//    }
}


